package com.example.administrator.personalmail.fragment;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

import com.example.administrator.personalmail.Activity.CreateOrderActivity;
import com.example.administrator.personalmail.Activity.LoginActivity;
import com.example.administrator.personalmail.MyApplication;
import com.example.administrator.personalmail.R;
import com.example.administrator.personalmail.adapter.CarAdapter;
import com.example.administrator.personalmail.api.Contants;
import com.example.administrator.personalmail.eventBean.CarFragment;
import com.example.administrator.personalmail.model.ShoppingCart;
import com.example.administrator.personalmail.msg.User;
import com.example.administrator.personalmail.widget.CartProvider;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

/**
 * Created by Administrator on 2018/4/14 0014.
 */

public class FragmentFour extends Fragment{
    private CartProvider mCartProvider;
    private RecyclerView mRecyclerView;
    private CarAdapter mCarAdapter;
    private View view;
    private Context mContext;
    private CheckBox checkBox_all;
    private TextView tvTotalPrice;
    private Button btEdit;
    private Button btOrder;
    private Button btDel;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_four,container,false);

        initShowData();
        return view;
    }

    public void initShowData() {
        checkBox_all = view.findViewById(R.id.checkbox_all);
        tvTotalPrice = view.findViewById(R.id.txt_total);
        mRecyclerView = view.findViewById(R.id.recycler_view_car);
        btEdit = view.findViewById(R.id.toolbar_edit_car);
        btOrder = view.findViewById(R.id.btn_order);
        btOrder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MyApplication application = MyApplication.getInstance();
                User user = application.getUser();
                if (user == null) {
                    Intent intent = new Intent(getActivity(), LoginActivity.class);
                    startActivity(intent);
                }else {
                    Intent intent = new Intent(getActivity(),CreateOrderActivity.class);
                    intent.putExtra("sum",mCarAdapter.getTotalPrice()+"");
                    startActivityForResult(intent,1);
                }
            }
        });
        btDel = view.findViewById(R.id.btn_del);

        mCartProvider = new CartProvider(getContext());
        List<ShoppingCart> list = mCartProvider.getAll();
        mCarAdapter = new CarAdapter(getContext(),list,checkBox_all,tvTotalPrice,btEdit,btOrder,btDel);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mRecyclerView.setAdapter(mCarAdapter);

        EventBus.getDefault().post(new CarFragment(this));

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Contants.REQUEST_SUCCESS){
            //TODO 订单已经提交完成
        }
    }

    public void refresh(){
        List<ShoppingCart> list = mCartProvider.getAll();
        mCarAdapter = new CarAdapter(getContext(),list,checkBox_all,tvTotalPrice,btEdit,btOrder,btDel);
        mRecyclerView.setAdapter(mCarAdapter);
    }
}
